package com.youuvs.org.controller;

import com.youuvs.basic.utils.JsonResult;
import com.youuvs.basic.utils.PageList;
import com.youuvs.org.domian.Shop;
import com.youuvs.org.query.ShopQuery;
import com.youuvs.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @GetMapping
    public List<Shop> findAll(){
        List<Shop> lists=shopService.findAll();
        return lists;
    }
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.insert(shop);
            }else{
                shopService.update(shop);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

    @PostMapping
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query){
        try {
            PageList<Shop> lists=shopService.queryPage(query);
           return lists;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    //@RequestBody 将参数放进请求体中
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            shopService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }

    }


}
